#include "key.h"

uint8_t Key_read(void)
{
    if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0) == 0)
        return  1;
    else if (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_1) == 0)
        return  2;
    else if (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_2) == 0)
        return  3;
    else if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == 0)
        return  4;
    else
        return 0;
}

uint8_t alarm_aray[3];
uint8_t RTC_set_count;
uint8_t alarm_set_count;
RTC_TimeTypeDef time_stop;
RTC_TimeTypeDef time_alarm;
uint8_t select_set;
uint8_t select_set_alarm;
uint8_t key_now, key_up, key_down, key_old;
uint16_t key_long;
void Key_proc(void)
{
    key_now = Key_read();
    key_down = key_now & (key_now ^ key_old);
    key_old = key_now;

    if(key_down)
    {
        key_long = 0;
        switch (key_down)
        {      
            case 1:if(show_mode == 0)
                    {
                        show_mode = 1;
                        select_set = 0;
                        RTC_set_count++;
                        AT24C02_Write_data(0, &RTC_set_count, 1);
                    }
                    else if(show_mode == 1)
                    {
                        show_mode = 0;
                    }
                    if (show_mode == 2)
                    {
                        select_set_alarm++;
                        if (select_set_alarm > 2)
                            select_set_alarm = 0;
                    }

                if(show_mode == 1)
                {
                    time_stop.Hours= time.Hours;
                    time_stop.Minutes= time.Minutes;
                    time_stop.Seconds = time.Seconds;
                }
                else if(show_mode == 0)
                {
                    HAL_RTC_SetTime(&hrtc, &time_stop, RTC_FORMAT_BIN);
                }
                break;
            case 2:if(show_mode == 0)
            {
                show_mode = 2;
                select_set_alarm = 0;
                alarm_set_count++;
                AT24C02_Write_data(1, &alarm_set_count, 1);
            }
            else if(show_mode == 2)
            {
                show_mode = 0;
                alarm_aray[0] = time_alarm.Hours;
                alarm_aray[1] = time_alarm.Minutes;
                alarm_aray[2] = time_alarm.Seconds;
                AT24C02_Write_data(2, alarm_aray, 3);
            }


                if(show_mode == 1)
                {
                    select_set++;
                    if(select_set > 2)
                        select_set = 0;
                }
                    

                break;
                //+
            case 3:
            
            
            if(show_mode == 1)
            {
            if(select_set == 0)
            {
                time_stop.Hours++;
                if (time_stop.Hours > 23)
                    time_stop.Hours = 0;
            }
            else if(select_set == 1)
            {
                time_stop.Minutes++;
                if (time_stop.Minutes > 59)
                    time_stop.Minutes = 0;
            }
            else if(select_set == 2)
            {
                time_stop.Seconds++;
                if (time_stop.Seconds > 59)
                    time_stop.Seconds = 0;
            }

            }
else if(show_mode == 2)
{
    if (select_set_alarm == 0)
    {
        time_alarm.Hours++;
        if (time_alarm.Hours > 23)
            time_alarm.Hours = 0;
    }
    else if (select_set_alarm == 1)
    {
        time_alarm.Minutes++;
        if (time_alarm.Minutes > 59)
            time_alarm.Minutes = 0;
    }
    else if (select_set_alarm == 2)
    {
        time_alarm.Seconds++;
        if (time_alarm.Seconds > 59)
            time_alarm.Seconds = 0;
    }
}


                break;
                //-
            case 4:


            if(show_mode == 1)
            {
                               if (select_set == 0)
                {
                    time_stop.Hours--;
                    if (time_stop.Hours > 25)
                        time_stop.Hours = 24;
                }
            else if (select_set == 1)
            {
                time_stop.Minutes--;
                if (time_stop.Minutes >60)
                    time_stop.Minutes = 59;
            }
            else if (select_set == 2)
            {
                time_stop.Seconds--;
                if (time_stop.Seconds >60)
                    time_stop.Seconds = 59;
            } 
            }
            else if(show_mode == 2)
            {
                if (select_set_alarm == 0)
                {
                    time_alarm.Hours--;
                    if (time_alarm.Hours >25)
                        time_alarm.Hours = 24;
                }
                else if (select_set_alarm == 1)
                {
                    time_alarm.Minutes--;
                    if (time_alarm.Minutes >60)
                        time_alarm.Minutes = 59;
                }
                else if (select_set == 2)
                {
                    time_alarm.Seconds--;
                    if (time_alarm.Seconds >60)
                        time_alarm.Seconds = 59;
                }
            }

                break;
                }
}
    if(key_old && key_long>=1000)
    {
        switch (key_old)
        {
        case 1:
            break;
        case 2:
            break;
            //+
        case 3:

            if (show_mode == 1)
            {
                if (select_set == 0)
                {
                    time_stop.Hours++;
                    if (time_stop.Hours > 23)
                        time_stop.Hours = 0;
                }
                else if (select_set == 1)
                {
                    time_stop.Minutes++;
                    if (time_stop.Minutes > 59)
                        time_stop.Minutes = 0;
                }
                else if (select_set == 2)
                {
                    time_stop.Seconds++;
                    if (time_stop.Seconds > 59)
                        time_stop.Seconds = 0;
                }
            }
            else if (show_mode == 2)
            {
                if (select_set_alarm == 0)
                {
                    time_alarm.Hours++;
                    if (time_alarm.Hours > 23)
                        time_alarm.Hours = 0;
                }
                else if (select_set_alarm == 1)
                {
                    time_alarm.Minutes++;
                    if (time_alarm.Minutes > 59)
                        time_alarm.Minutes = 0;
                }
                else if (select_set_alarm == 2)
                {
                    time_alarm.Seconds++;
                    if (time_alarm.Seconds > 59)
                        time_alarm.Seconds = 0;
                }
            }

            break;
            //-
        case 4:

            if (show_mode == 1)
            {
                if (select_set == 0)
                {
                    time_stop.Hours--;
                    if (time_stop.Hours >25)
                        time_stop.Hours = 24;
                }
                else if (select_set == 1)
                {
                    time_stop.Minutes--;
                    if (time_stop.Minutes >60)
                        time_stop.Minutes = 59;
                }
                else if (select_set == 2)
                {
                    time_stop.Seconds--;
                    if (time_stop.Seconds >60)
                        time_stop.Seconds = 59;
                }
            }
            else if (show_mode == 2)
            {
                if (select_set_alarm == 0)
                {
                    time_alarm.Hours--;
                    if (time_alarm.Hours > 25)
                        time_alarm.Hours = 24;
                }
                else if (select_set_alarm == 1)
                {
                    time_alarm.Minutes--;
                    if (time_alarm.Minutes>60)
                        time_alarm.Minutes = 59;
                }
                else if (select_set_alarm == 2)
                {
                    time_alarm.Seconds--;
                    if (time_alarm.Seconds>60)
                        time_alarm.Seconds = 59;
                }
            }
            break;
        }
    }
}

    


